﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace h1
{
    public class BaiduCrawler
    {
        private string baidu = @"https://www.baidu.com/s?wd=";
        public string Word  { get; set; }
        public string Crawl()
        {
            string url = baidu + Word;
            try
            {
                string html = Download(url);
                html = Prase(html);
                return html;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        private string Download(string url)
        {
            try
            {
                WebClient client = new WebClient();
                client.Encoding = Encoding.UTF8;
                string html=client.DownloadString(url);
                return html;
            }
            catch(Exception ex)
            {
                return ex.Message;
            }
        }
        private string Prase(string html)
        {
            string buf = "[\u4e00-\u9fa5]";
            MatchCollection matches = new Regex(buf).Matches(html);
            string result = null;
            foreach (Match match in matches)
            {
                result += match.Value;
                if (result.Length >= 200)
                    break;
            }
            return result;
        }
    }
}
